package tests;

import java.io.FileInputStream;
import java.net.Socket;
import java.util.Properties;

import cs236369.proxy.ProxyServer;

import junit.framework.TestCase;


public class Hw3Tests extends TestCase {

	
	private ProxyServer proxy;
	private int proxyport;
	
	public Hw3Tests() throws Exception {
		Properties p = new Properties();
		
		p.load(new FileInputStream("config"));
		proxy = new ProxyServer(p);
		proxy.createCacheTable();
		proxyport = Integer.parseInt(p.getProperty("proxy.port"));
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				proxy.start();
			}
		}).start();
		
		// wait for proxy to init
		Thread.sleep(3000);

	}
	
	
	public void test1() throws Exception {
		String res = "HTTP/1.1 200 OK\r\n" +
			"Cache-Control: no-cache\r\n" +
			"Content-Length: 10\r\n" +
			"\r\n" +
			"1234567890";
			
		TestServer ts = new TestServer(res, 54321);
		Socket s = null;
		Thread t = new Thread(ts);
		t.start();
		try {
			s = new Socket("localhost", proxyport);
			String req = "GET http://localhost:54321/x HTTP/1.1\r\n" +
					"Host: localhost:54321\r\n" +
					"Cache-Control: no-cache\r\n" +
					"\r\n";
			s.getOutputStream().write(req.getBytes());
			s.getOutputStream().flush();	
		} finally {
			try { s.close(); } catch (Exception e) {}
			t.join();
		}
		System.out.println(ts.getRequest());
		assertFalse(ts.getRequest().isEmpty());
	}
}
